package com.ruoyi.system.util;

import com.ruoyi.common.utils.poi.ExcelHandlerAdapter;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
 * 功能描述
 *
 * @author: adam
 * @date: 2024年07月04日 17:39
 */
public class MyDataHandler implements ExcelHandlerAdapter
{
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Object format(Object value, String[] args, Cell cell, Workbook wb)
    {
        Sheet sheet = wb.getSheetAt(0);
//        sheet.autoSizeColumn(cell.getColumnIndex());
//        logger.info("============MyDataHandlervalue:"+value.toString());
        if (value.equals("填表说明"))
        {
            value="填表说明:1、板材’管材报价按重量报价，单位填写为元/Kg;2、锻件报价按单件报价，单位填写为元/件";
            // 确定合并的起始列（cell所在的列）
            int firstColumn = cell.getColumnIndex();
            int lastColumn = firstColumn + 5; // 注意索引是从0开始的
            // 合并当前行的这几列
            int firstRow = cell.getRow().getRowNum();
            int lastRow = firstRow; // 因为只合并一行，所以起始行和结束行相同
            sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstColumn, lastColumn - 1)); // 注意CellRangeAddress的列索引是到结束列的前一个
            // 自定义用户名为若依/单元格文字设置为红色
            CellStyle style = wb.createCellStyle();
            style.setWrapText(false);
            style.setAlignment(HorizontalAlignment.LEFT);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            Font dataFont = wb.createFont();
            dataFont.setFontName("Arial");
            dataFont.setFontHeightInPoints((short) 10);
            dataFont.setColor(IndexedColors.RED.index);
            style.setFont(dataFont);
            cell.setCellStyle(style);
        }
//        logger.info("============MyDataHandlervalue2:"+value.toString());
        return value;
    }
}
